<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房间状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择房间状态" clearable>
          <el-option
            v-for="dict in dict.type.user_room_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间类型" prop="roomTypeName">
        <el-select v-model="queryParams.roomTypeName" placeholder="请选择房间类型" clearable>
          <el-option
            v-for="dict in dict.type.user_room_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间床数" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入房间床数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotel:hlroom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hotel:hlroom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hotel:hlroom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotel:hlroom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hlroomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间号" align="center" prop="id" />
      <el-table-column label="房间类型" align="center" prop="roomType.name" />
      <el-table-column label="价格详情" align="center">
        <template slot-scope="scope">
          <el-button type="info" plain size="small" @click="catPrice(scope.row)">查看价格</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="钟点价 / 小时 [ 普通 / 白银 / 黄金 / 铂金 / 钻石 ]" align="center" prop="stayTypeIsZeroVO.gold" width="350"/>-->
      <el-table-column label="设备详情" align="center">
        <template slot-scope="scope">
          <el-button type="info" plain size="small" @click="catDevice(scope.row)">查看设备</el-button>
        </template>
      </el-table-column>
      <el-table-column label="房间状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_room_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="夜宿价 / 天 [ 普通 / 白银 / 黄金 / 铂金 / 钻石 ]" align="center" prop="stayTypeIsOneVO" width="350"/>-->
      <el-table-column label="押金" align="center" prop="roomType.deposit" />
      <el-table-column label="房间床数" align="center" prop="num" />
      <el-table-column label="修改者" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hotel:hlroom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotel:hlroom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!--    查看设备详细信息按钮弹出对话框-->
    <el-dialog
      :title="tempTitle"
      :visible.sync="catDevices"
      width="570px"
      center>

      <table style="background-color: whitesmoke">
        <tr style="text-align: center">
          <td style="background-color: #888a8e;width: 340px;color: white">设备名称</td>
          <td style="background-color: #888a8e;width: 180px;color: white">设备数量</td>
        </tr>
      </table>

      <div v-for="(item, index) of deviceCountList">
        <div v-if="index % 2 === 0">
          <table>
            <tr style="text-align: center">
              <td style="width: 340px;border: 1px solid gainsboro">{{item.name}}</td>
              <td style="width: 180px;border: 1px solid gainsboro">{{item.count}}</td>
            </tr>
          </table>
        </div>
        <div v-else>
          <table>
            <tr style="text-align: center">
              <td style="width: 340px;border: 1px solid gainsboro;background-color: whitesmoke">{{item.name}}</td>
              <td style="width: 180px;border: 1px solid gainsboro;background-color: whitesmoke">{{item.count}}</td>
            </tr>
          </table>
        </div>
      </div>

      <span slot="footer" class="dialog-footer" style="margin-left: 450px">
        <el-button type="info" @click="catDevices = false" size="small">确 定</el-button>
      </span>
    </el-dialog>



    <!--    查看价格详细信息按钮弹出对话框-->
    <el-dialog
      :title="tempTitle"
      :visible.sync="catPrices"
      width="785px"
      center>

      <table style="background-color: whitesmoke">

        <tr style="text-align: center">
          <td style="background-color: #888a8e;width: 120px;color: white">价格详情</td>
          <td style="background-color: #888a8e;width: 120px;color: white">普通会员</td>
          <td style="background-color: #888a8e;width: 120px;color: white">白银会员</td>
          <td style="background-color: #888a8e;width: 120px;color: white">黄金会员</td>
          <td style="background-color: #888a8e;width: 120px;color: white">铂金会员</td>
          <td style="background-color: #888a8e;width: 120px;color: white">钻石会员</td>
        </tr>
      </table>

      <table>
        <tr style="text-align: center">
          <td style="width: 120px;border: 1px solid #888a8e">钟点价格</td>
          <td style="width: 120px;border: 1px solid #888a8e">￥{{tempPrice.zero.ordinary}}</td>
          <td style="width: 120px;border: 1px solid #888a8e">￥{{tempPrice.zero.silver}}</td>
          <td style="width: 120px;border: 1px solid #888a8e">￥{{tempPrice.zero.gold}}</td>
          <td style="width: 120px;border: 1px solid #888a8e">￥{{tempPrice.zero.platinum}}</td>
          <td style="width: 120px;border: 1px solid #888a8e">￥{{tempPrice.zero.diamond}}</td>
        </tr>
      </table>

      <table>
        <tr style="text-align: center">
          <td style="width: 120px;border: 1px solid #888a8e;background-color: #ebeef5">夜宿价格</td>
          <td style="width: 120px;border: 1px solid #888a8e;background-color: #ebeef5">￥{{tempPrice.one.ordinary}}</td>
          <td style="width: 120px;border: 1px solid #888a8e;background-color: #ebeef5">￥{{tempPrice.one.silver}}</td>
          <td style="width: 120px;border: 1px solid #888a8e;background-color: #ebeef5">￥{{tempPrice.one.gold}}</td>
          <td style="width: 120px;border: 1px solid #888a8e;background-color: #ebeef5">￥{{tempPrice.one.platinum}}</td>
          <td style="width: 120px;border: 1px solid #888a8e;background-color: #ebeef5">￥{{tempPrice.one.diamond}}</td>
        </tr>
      </table>

      <span slot="footer" class="dialog-footer" style="margin-left: 670px">
        <el-button type="info" @click="catPrices = false" size="small">确 定</el-button>
      </span>
    </el-dialog>


    <!-- 添加或修改房间列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间id" prop="id">
          <el-input v-model="form.id" placeholder="请输入房间id" />
        </el-form-item>
        <el-form-item label="房间类型id" prop="roomTypeId">
          <el-input v-model="form.roomTypeId" placeholder="请输入房间类型id" />
        </el-form-item>
        <el-form-item label="房间状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择房间状态">
            <el-option
              v-for="dict in dict.type.user_room_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备个数" prop="deviceCount">
          <el-input v-model="form.deviceCount" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建者" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="修改者" prop="updateBy">
          <el-input v-model="form.updateBy" placeholder="请输入修改者" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHlroom, getHlroom, delHlroom, addHlroom, updateHlroom } from "@/api/hotel/hlroom";
import {DataHlroomdeviceByRoomTypeName} from "@/api/hotel/hlroomdevice";

export default {
  name: "Hlroom",
  dicts: ['user_room_status','user_room_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 临时title
      tempTitle:"",
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间列表表格数据
      hlroomList: [],
      // 酒店房间设备查询数据
      deviceCountList: [],
      // 价格临时变量
      tempPrice: {
        zero:{},
        one:{}
      },
      // 弹出层标题
      title: "",
      // 查看设备开关
      catDevices:false,
      // 查看价格开关
      catPrices:false,
      // 是否显示弹出层
      open: false,
      // 修改者时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        roomTypeName: null,
        num: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {



    // 打开查看设备
    catDevice(row){
      // 清空数据
      this.tempTitle = ""
      this.deviceCountList = [];
      // 将值赋值给该变量
      this.tempTitle = row.roomType.name+"设备数量详细信息"
      DataHlroomdeviceByRoomTypeName(row.roomType.name).then(response =>{
        this.deviceCountList = JSON.parse(response.data[0].deviceNameAndCount);
        this.catDevices = true;
      })
    },


    // 打开查看价格
    catPrice(row){
      // 清空数据
      this.resetTempPrice();
      // 将值赋值给该变量
      this.tempTitle = row.roomType.name+"价格详细信息"
      this.tempPrice.zero = row.stayTypeIsZeroVO;
      this.tempPrice.one = row.stayTypeIsOneVO;
      this.catPrices = true;
    },


    /** 查询房间列表列表 */
    getList() {
      this.loading = true;
      listHlroom(this.queryParams).then(response => {
        this.hlroomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.resetTempPrice();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomTypeId: null,
        stayTypeIsZeroVO: null,
        stayTypeIsOneVO: null,
        num: null,
        state: null,
        deviceId: null,
        deviceCount: null,
        roomType:null,
        roomPrice:null,
        device:null,
        remark: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    // 临时价格重置
    resetTempPrice() {
      this.tempPrice = {
        zero: {},
        one:{}
      };
      this.resetForm("tempPrice");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.resetTempPrice();
      this.open = true;
      this.title = "添加房间列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.resetTempPrice();
      const id = row.id || this.ids
      getHlroom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房间列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHlroom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHlroom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房间列表编号为"' + ids + '"的数据项？').then(function() {
        return delHlroom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hotel/hlroom/export', {
        ...this.queryParams
      }, `hlroom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
